package com.dongdong.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdong.mapper.TbItemCatMapper;
import com.dongdong.model.TbItemCat;
import com.dongdong.model.TbItemCatExample;
import com.dongdong.page.PageParams;
import com.dongdong.service.ItemCatService;
import com.dongdong.service.SpecOptionService;
import com.dongdong.service.TypeTemplateService;
import com.dongdong.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Autowired
    private TypeTemplateService typeTemplateService;

    @Autowired
    private SpecOptionService specOptionService;

    @Override
    public List<ItemCatVO> getItemCatByParentId(Long parentId) {
        TbItemCatExample example = new TbItemCatExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        List<TbItemCat> tbItemCats = itemCatMapper.selectByExample(example);
        List<ItemCatVO> collect = tbItemCats.stream().map(itemlist -> {
            ItemCatVO vo = new ItemCatVO();
            BeanUtils.copyProperties(itemlist, vo);
            return vo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageParams<TbItemCat> getItemParentXiaji(Integer pageNum, Integer pageSize, Long parentId) {
        PageHelper.startPage(pageNum, pageSize);
        TbItemCatExample example = new TbItemCatExample();
        if (parentId != null) {
            example.createCriteria().andParentIdEqualTo(parentId);
        }
        List<TbItemCat> tbItemCats = itemCatMapper.selectByExample(example);
        PageInfo<TbItemCat> info = new PageInfo<>(tbItemCats);
        return new PageParams<>(info.getPageNum(), info.getPageSize(), info.getList(), info.getTotal(), info.getPages());
        //注意：将参数传进page类的时候一定要注意顺序
    }

    @Override
    public int InsertItemCat(ItemCatVO itemCatVO) {
        TbItemCat cat = new TbItemCat();
        BeanUtils.copyProperties(itemCatVO, cat);
        int insert = itemCatMapper.insert(cat);
        return insert;
    }

    @Override
    public ItemCatVO getItemCatById(Long id) {
        TbItemCat tbItemCat = itemCatMapper.selectByPrimaryKey(id);
        if (tbItemCat == null) {
            return null;
        }
        ItemCatVO vo = new ItemCatVO();
        BeanUtils.copyProperties(tbItemCat, vo);
        return vo;
    }

    @Override
    public ItemCatVO getPatentIdShow(Long parenId) {
        TbItemCatExample example = new TbItemCatExample();
        example.createCriteria().andParentIdEqualTo(parenId);
        List<TbItemCat> tbItemCats = itemCatMapper.selectByExample(example);
        List<ItemCatVO> collect = tbItemCats.stream().map(item -> {
            ItemCatVO vo = new ItemCatVO();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());

        //如果有数据就返回第一条
        if (collect != null && collect.size() >= 0) {
            return collect.get(0);
        }
        return null;
    }

    @Override
    public TypeVO getBrand(Long id) {
        TbItemCat tbItemCat = itemCatMapper.selectByPrimaryKey(id);//根据id去查询itemcat表  typeid  返回分类的内容

        //根据上面获取到的typeid去模板表查询
        TypeVO typeVO = typeTemplateService.getTypeById(tbItemCat.getTypeId());

        //----------------------------------------拼接规格---------------------------------------
        //获取spec的list集合
        String specIds = typeVO.getSpecIds();
        List<SpecVO> specVOS = JSONObject.parseArray(specIds, SpecVO.class);//转成对象

        //获取所有规格选项
        List<SpecOptionVO> optionList = specOptionService.getOptionList();

        //相同的id对应多个不同的选项可以这样写    根据id分组
        Map<Long, List<SpecOptionVO>> opsMap = optionList.stream().collect(Collectors.groupingBy(x -> x.getSpecId()));

        //循环
        for (SpecVO specVO : specVOS) {
            Long specId = specVO.getId();
            List<SpecOptionVO> optionVOList = opsMap.get(specId);
            specVO.setSpecOptionVOList(optionVOList);
        }

        typeVO.setSpecVOSList(specVOS);//SpecVO  和 TypeVO 都将规格选项放进去
        return typeVO;
    }

}
